package top.topsmart.api.bycenter.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import top.topsmart.api.bycenter.util.FileUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
import java.util.stream.Stream;

/**
 *文件上传的Controller
 * @author lrd
 * @date 2018/10/22
 * @param  * @param null
 * @return null
 */
@RestController
@RequestMapping(value = "upload",method = {RequestMethod.GET,RequestMethod.POST})
public class UploadController {
    @RequestMapping(value = "/upload",method = RequestMethod.GET)
    public String upload(){
        return "/upload";
    }

    /**
     *单文件上传
     * @param file
     * @return String
     */
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file")MultipartFile file,HttpServletRequest request){
        String contentType = file.getContentType();   //图片文件类型
        String fileName = FileUtil.getFileName(file.getOriginalFilename());  //图片名字
        String filePath = "C:\\Users\\13500\\IdeaProjects\\bycenter\\src\\main\\resources\\upload\\";
        try {
            //调用文件处理类FileUtil，处理文件，将文件写入指定位置
            FileUtil.uploadFile(file.getBytes(),filePath,fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filePath;
    }

}
